package org.jeecgframework.web.controller;

import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.util.*;
import org.jeecgframework.web.service.*;
import org.jeecgframework.web.system.pojo.base.*;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Scope("prototype")
@Controller
@RequestMapping("/cbsServiceReportController")
public class CbsServiceReportController extends BaseController {

    private static final Logger logger = Logger.getLogger(CbsServiceReportController.class);
    @Autowired
    private CbsInspectionEvaluateServiceI cbsInspectionEvaluateService;
    @Autowired
    private CbsServiceReportServiceI cbsServiceReportService;
    @Autowired
    private CbsServiceTypeServiceI cbsServiceTypeService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private CbsServiceServiceI cbsServiceServiceI;
    @Autowired
    private CbsServiceLogServiceI cbsServiceLogServiceI;
    @Autowired
    private CbsAttachTaskServiceI cbsAttachTaskService;
    @Autowired
    private CbsServiceAssignListServiceI cbsServiceAssignListServiceI;
    @Autowired
    private WxCommonService wxCommonService;
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * 查看完工单
     */
    @RequestMapping(params = "lookReport")
    public ModelAndView lookReport(CbsServiceEntity cbsService, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtil.isNotEmpty(id)) {
            //查询服务单
            cbsService = cbsServiceServiceI.get(CbsServiceEntity.class, id);
            request.setAttribute("cbsService", cbsService);
            if (StringUtil.isNotEmpty(id)) {
                List<Object[]> arrivalofabnormalEntities = systemService.findListbySql("select * from arrival_of_abnormal where service_id = '" + id + "' ORDER BY create_date DESC  LIMIT 1;");
                if (arrivalofabnormalEntities != null && arrivalofabnormalEntities.size() > 0) {
                    Integer num = (Integer) arrivalofabnormalEntities.get(0)[2];
                    request.setAttribute("signNumbers", "" + num + "次");
                    request.setAttribute("dateList", arrivalofabnormalEntities.get(0)[7]);
                } else {
                    request.setAttribute("signNumbers", "无");
                }
            }
            //查询服务单ID查询完工单
            CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id='" + id + "'");
            request.setAttribute("cbsServiceReportEntity", cbsServiceReportEntity);
            if (cbsServiceReportEntity != null) {
                //查询附件
                List<ZyFileLogEntity> zyFileLogEntityList = systemService.findHql("from ZyFileLogEntity where businessKey = '" + id + "'");
                //机型
                ZyEquipmentTypeEntity zyEquipmentTypeEntity = systemService.singleResult("from ZyEquipmentTypeEntity where id = '" + cbsServiceReportEntity.getEquipmentType() + "'");
                request.setAttribute("equipmentType", zyEquipmentTypeEntity.getTypeDesc());
                //激光改造
                TSType tsType = systemService.singleResult("from TSType where typecode='" + cbsServiceReportEntity.getJgReform() + "' and TSTypegroup.typegroupcode ='jg_reform'");
                request.setAttribute("jgReform", tsType.getTypename());
                //激光改造
                TSType tsType1 = systemService.singleResult("from TSType where typecode='" + cbsServiceReportEntity.getLwzk() + "' and TSTypegroup.typegroupcode ='lwzk'");
                request.setAttribute("lwqk", tsType1.getTypename());
                //获取备件更新情况
                if (cbsServiceReportEntity != null) {
                    List<CbsSceneEquipmentUseEntity> cbsSceneEquipmentUseEntity = systemService.findHql("from CbsSceneEquipmentUseEntity where sceneReportId.id='" + cbsServiceReportEntity.getId() + "'");
                    request.setAttribute("cbsSceneEquipmentUseEntity", cbsSceneEquipmentUseEntity);
                }
            }
        }
        return new ModelAndView("com/zybk/cbsServiceReport/FinishPage");
    }

    /**
     * 现场服务完工报告跳转
     *
     * @return
     */
    @RequestMapping(params = "cbsServiceReportXC")
    public ModelAndView cbsServiceType(CbsServiceEntity cbsService, HttpServletRequest req) {
        String type = req.getParameter("type"); //HD提交的完工单
        req.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
        //是否为补填完工单
        Boolean editReport = Boolean.valueOf(oConvertUtils.getString(req.getParameter("editReport")));
        req.setAttribute("editReport", editReport);

        String id = req.getParameter("id");
        //获取登录的用户信息
        TSUser user = ResourceUtil.getSessionUserName();
        //用户的角色集合
        List<String> roleList = new ArrayList<>();
        List<TSRoleUser> tsRoleUserList = systemService.findHql("from TSRoleUser where TSUser.id='" + user.getId() + "'");
        for (TSRoleUser tsRoleUser : tsRoleUserList) {
            String role = tsRoleUser.getTSRole().getRoleCode();
            roleList.add(role);
        }
        if (roleList.contains("barcoHelpdesk")) {
            req.setAttribute("typeRole", "ser");
        }
        if (roleList.contains("admin")) {
            req.setAttribute("typeRole", "ser");
        }
        List<TSTerritory> territoryList = null;
        List<TSType> tsTypes = null;
        List<TSType> tsTypes2 = null;
        List<TSType> jgs = null;
        List<TSType> lws = null;
        List<TSType> gcq = null;
        List<TSType> photos = null;
        cbsService = cbsServiceServiceI.get(CbsServiceEntity.class, id);
        //预约上门时间
        String orderTime = null;
        if (cbsService.getOrderTimeAgin() != null) {
            orderTime = String.valueOf(cbsService.getOrderTimeAgin());
        }
        if (cbsService.getOrderTimeAgin() == null && cbsService.getOrderTime() != null) {
            orderTime = String.valueOf(cbsService.getOrderTime());
        }
        req.setAttribute("orderTime", orderTime);

        //获取服务单中sn所属的类型
        ZyEquipmentSpareEntity zyEquipmentSpareEntity = systemService.singleResult("from ZyEquipmentSpareEntity where sn = '" + cbsService.getSn() + "'");
        if (zyEquipmentSpareEntity != null) {
            ZyEquipmentTypeEntity zyEquipmentTypeEntity = systemService.singleResult("from ZyEquipmentTypeEntity where id = '" + zyEquipmentSpareEntity.getType().getId() + "'");
            req.setAttribute("zyEquipmentTypeEntity", zyEquipmentTypeEntity);
            req.setAttribute("zyEquipmentSpareEntity", zyEquipmentSpareEntity);
        }
        //获取附加任务
        List<CbsAttachTaskEntity> cbsAttachTaskEntities = new ArrayList<>();
        List<CbsServiceTaskMapEntity> cbsServiceTaskMapEntityList = systemService.findHql("from CbsServiceTaskMapEntity where serviceId.id='" + id + "' and type is null");
        if (cbsServiceTaskMapEntityList != null && cbsServiceTaskMapEntityList.size() > 0) {
            for (CbsServiceTaskMapEntity serviceTaskMapEntity : cbsServiceTaskMapEntityList) {
                List<CbsAttachTaskEntity> cbsAttachTaskEntityList = systemService.findHql("from CbsAttachTaskEntity where id='" + serviceTaskMapEntity.getAttachTaskId().getId() + "'");
                cbsAttachTaskEntities.addAll(cbsAttachTaskEntityList);
            }
            req.setAttribute("cbsAttachTaskEntities", cbsAttachTaskEntities);
        }
        territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");

        req.setAttribute("territoryList", territoryList);

        req.setAttribute("cbsService", cbsService);
        tsTypes = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='FWQPP'");
        req.setAttribute("types", tsTypes);
        tsTypes2 = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='GYPP'");
        req.setAttribute("types2", tsTypes2);
        jgs = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='jg_reform'");
        req.setAttribute("jgs", jgs);
        lws = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='lwzk'");
        req.setAttribute("lws", lws);
        gcq = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='GCQ'");
        req.setAttribute("gcq", gcq);
        photos = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='photoTypesOf'");
        req.setAttribute("photos", photos);
        List<Map<String, Object>> mapList = systemService.findForJdbc("select id,type_desc from zy_equipment_type where parent_id in (select id from zy_equipment_type where id  in (select id from zy_equipment_type where parent_id ='402883f3545bca4101545be730e0000b'))");
        req.setAttribute("mapList", mapList);

        //回显附件
        if (cbsService != null) {
            List<ZyFileLogEntity> zyFileLogEntityList = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC1'");
            req.setAttribute("zyFileLogEntityList", zyFileLogEntityList);
            List<ZyFileLogEntity> zyFileLogEntityList2 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC2'");
            req.setAttribute("zyFileLogEntityList2", zyFileLogEntityList2);
            List<ZyFileLogEntity> zyFileLogEntityList3 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC3'");
            req.setAttribute("zyFileLogEntityList3", zyFileLogEntityList3);
            List<ZyFileLogEntity> zyFileLogEntityList4 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC4'");
            req.setAttribute("zyFileLogEntityList4", zyFileLogEntityList4);
            List<ZyFileLogEntity> zyFileLogEntityList5 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC5'");
            req.setAttribute("zyFileLogEntityList5", zyFileLogEntityList5);
            List<ZyFileLogEntity> zyFileLogEntityList6 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC6'");
            req.setAttribute("zyFileLogEntityList6", zyFileLogEntityList6);
            List<ZyFileLogEntity> zyFileLogEntityList7 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC7'");
            req.setAttribute("zyFileLogEntityList7", zyFileLogEntityList7);
        }
        CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id='" + id + "'");
        List<Map<String, Object>> maps = systemService.findForJdbc("SELECT zy_equipment_type.id,zy_equipment_type.type_desc FROM zy_equipment_type LEFT JOIN zy_es_item i ON zy_equipment_type.id = i.type WHERE i.is_track = \"1\" and parent_id in (select id from zy_equipment_type where type_desc= '放映机备件') GROUP BY zy_equipment_type.type_desc ");
        req.setAttribute("maps", maps);
        //获取备件使用情况
        if (cbsServiceReportEntity != null) {
            List<CbsSceneEquipmentUseEntity> cbsSceneEquipmentUseEntityList = systemService.findHql("from CbsSceneEquipmentUseEntity where sceneReportId.id = '" + cbsServiceReportEntity.getId() + "'");
            req.setAttribute("cbsSceneEquipmentUseEntityList", cbsSceneEquipmentUseEntityList);
        }
        if (StringUtil.isNotEmpty(id)) {
            List<Object[]> arrivalofabnormalEntities = systemService.findListbySql("select * from arrival_of_abnormal where service_id = '" + id + "' ORDER BY create_date DESC  LIMIT 1;");
            if (arrivalofabnormalEntities != null && arrivalofabnormalEntities.size() > 0) {
                Integer num = (Integer) arrivalofabnormalEntities.get(0)[2];
                req.setAttribute("signNumbers", "" + num + "次");
                req.setAttribute("dateList", arrivalofabnormalEntities.get(0)[7]);
            } else {
                req.setAttribute("signNumbers", "无");
            }
        }
        if (cbsServiceReportEntity != null) {
            req.setAttribute("cbsServiceReportEntity", cbsServiceReportEntity);
            return new ModelAndView("com/zybk/cbsServiceReport/referOrders-show");
        } else {
            return new ModelAndView("com/zybk/cbsServiceReport/referOrders");
        }
    }

    @ResponseBody
    @RequestMapping(params = "getRole")
    public AjaxJson getRole(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            String id = request.getParameter("id");
            Map<String, Object> map = cbsServiceReportService.getCharacter(id);
            if (map != null && map.size() > 0) {
                ajaxJson.setAttributes(map);
            } else {
                ajaxJson.setAttributes(null);
            }

        } catch (Exception e) {
            e.getMessage();
        }
        return ajaxJson;
    }

    /**
     * 远程服务完工报告跳转
     *
     * @return
     */
    @RequestMapping(params = "RemoteService")
    public ModelAndView RemoteService(CbsServiceEntity cbsService, HttpServletRequest req) {
        //是否为补填完工单
        Boolean editReport = Boolean.valueOf(oConvertUtils.getString(req.getParameter("editReport")));
        req.setAttribute("editReport", editReport);

        String type = req.getParameter("type");
        req.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
        String id = req.getParameter("id");
        List<TSTerritory> territoryList = null;
        List<TSType> tsTypes = null;
        List<TSType> tsTypes2 = null;
        List<TSType> jgs = null;
        List<TSType> lws = null;
        List<TSType> gcq = null;
        //获取登录的用户信息
        TSUser user = ResourceUtil.getSessionUserName();
        //用户的角色集合
        List<String> roleList = new ArrayList<>();
        List<TSRoleUser> tsRoleUserList = systemService.findHql("from TSRoleUser where TSUser.id='" + user.getId() + "'");
        for (TSRoleUser tsRoleUser : tsRoleUserList) {
            String role = tsRoleUser.getTSRole().getRoleCode();
            roleList.add(role);
        }
        if (roleList.contains("barcoHelpdesk")) {
            req.setAttribute("typeRole", "ser");
        }
        if (roleList.contains("admin")) {
            req.setAttribute("typeRole", "ser");
        }
        cbsService = cbsServiceServiceI.get(CbsServiceEntity.class, id);

        //获取服务单中sn所属的类型
        ZyEquipmentSpareEntity zyEquipmentSpareEntity = systemService.singleResult("from ZyEquipmentSpareEntity where sn = '" + cbsService.getSn() + "'");
        if (zyEquipmentSpareEntity != null) {
            ZyEquipmentTypeEntity zyEquipmentTypeEntity = systemService.singleResult("from ZyEquipmentTypeEntity where id = '" + zyEquipmentSpareEntity.getType().getId() + "'");
            req.setAttribute("zyEquipmentTypeEntity", zyEquipmentTypeEntity);
            req.setAttribute("zyEquipmentSpareEntity", zyEquipmentSpareEntity);
        }
        territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
        req.setAttribute("territoryList", territoryList);
        req.setAttribute("cbsService", cbsService);
        tsTypes = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='FWQPP'");
        req.setAttribute("types", tsTypes);
        tsTypes2 = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='GYPP'");
        req.setAttribute("types2", tsTypes2);
        jgs = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='jg_reform'");
        req.setAttribute("jgs", jgs);
        lws = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='lwzk'");
        req.setAttribute("lws", lws);
        gcq = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='GCQ'");
        req.setAttribute("gcq", gcq);
        List<Map<String, Object>> mapList = systemService.findForJdbc("select id,type_desc from zy_equipment_type where parent_id in (select id from zy_equipment_type where id  in (select id from zy_equipment_type where parent_id ='402883f3545bca4101545be730e0000b'))\n");
        req.setAttribute("mapList", mapList);
        List<TSType> photos = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='photoTypesOf'");
        req.setAttribute("photos", photos);
        List<CbsServiceTaskMapEntity> cbsServiceTaskMapEntityList = systemService.findHql("from CbsServiceTaskMapEntity where serviceId.id='" + id + "' and type is null");
        List<CbsAttachTaskEntity> cbsAttachTaskEntities = new ArrayList<>();
        if (cbsServiceTaskMapEntityList != null && cbsServiceTaskMapEntityList.size() > 0) {
            for (CbsServiceTaskMapEntity serviceTaskMapEntity : cbsServiceTaskMapEntityList) {
                List<CbsAttachTaskEntity> cbsAttachTaskEntityList = systemService.findHql("from CbsAttachTaskEntity where id='" + serviceTaskMapEntity.getAttachTaskId().getId() + "'");
                cbsAttachTaskEntities.addAll(cbsAttachTaskEntityList);
            }
            req.setAttribute("cbsAttachTaskEntities", cbsAttachTaskEntities);
        }
        //回显附件
        if (cbsService != null) {
            List<ZyFileLogEntity> zyFileLogEntityList = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC1'");
            req.setAttribute("zyFileLogEntityList", zyFileLogEntityList);
            List<ZyFileLogEntity> zyFileLogEntityList2 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC2'");
            req.setAttribute("zyFileLogEntityList2", zyFileLogEntityList2);
            List<ZyFileLogEntity> zyFileLogEntityList3 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC3'");
            req.setAttribute("zyFileLogEntityList3", zyFileLogEntityList3);
            List<ZyFileLogEntity> zyFileLogEntityList4 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC4'");
            req.setAttribute("zyFileLogEntityList4", zyFileLogEntityList4);
            List<ZyFileLogEntity> zyFileLogEntityList5 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC5'");
            req.setAttribute("zyFileLogEntityList5", zyFileLogEntityList5);
            List<ZyFileLogEntity> zyFileLogEntityList6 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC6'");
            req.setAttribute("zyFileLogEntityList6", zyFileLogEntityList6);
            List<ZyFileLogEntity> zyFileLogEntityList7 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC7'");
            req.setAttribute("zyFileLogEntityList7", zyFileLogEntityList7);
        }
        CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id='" + id + "'");
        List<Map<String, Object>> maps = systemService.findForJdbc("SELECT zy_equipment_type.id,zy_equipment_type.type_desc FROM zy_equipment_type LEFT JOIN zy_es_item i ON zy_equipment_type.id = i.type WHERE i.is_track = \"1\" and parent_id in (select id from zy_equipment_type where type_desc= '放映机备件') GROUP BY zy_equipment_type.type_desc ");
        req.setAttribute("maps", maps);
        //获取备件使用情况
        if (cbsServiceReportEntity != null) {
            List<CbsSceneEquipmentUseEntity> cbsSceneEquipmentUseEntityList = systemService.findHql("from CbsSceneEquipmentUseEntity where sceneReportId.id = '" + cbsServiceReportEntity.getId() + "'");
            req.setAttribute("cbsSceneEquipmentUseEntityList", cbsSceneEquipmentUseEntityList);
        }
        if (cbsServiceReportEntity != null) {
            req.setAttribute("cbsServiceReportEntity", cbsServiceReportEntity);
            return new ModelAndView("com/zybk/cbsServiceReport/remoteService-show");
        } else {
            return new ModelAndView("com/zybk/cbsServiceReport/RemoteService");
        }
    }

    /**
     * 根据code值获取市
     */
    @RequestMapping(params = "getCityByCode")
    @ResponseBody
    public AjaxJson getCityByCode(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            String code = request.getParameter("province");
            String hql = "from TSTerritory where territoryLevel = 2 and TSTerritory.territoryCode = '" + code + "' ";
            List<TSTerritory> territoryList = systemService.findHql(hql);
            ajaxJson.setObj(territoryList);
        } catch (Exception e) {
            e.getMessage();
        }
        return ajaxJson;
    }

    /**
     * 引擎翻新现场检测完工单
     */
    @RequestMapping(params = "goEngineRefurbished")
    public ModelAndView goEngineRefurbished(HttpServletRequest request) {
        try {
            //是否为补填完工单
            Boolean editReport = Boolean.valueOf(oConvertUtils.getString(request.getParameter("editReport")));
            request.setAttribute("editReport", editReport);

            String type = request.getParameter("type");
            request.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.get(CbsServiceEntity.class, id);
            if (cbsServiceEntity != null) {
                request.setAttribute("cbsServiceEntity", cbsServiceEntity);
                List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsServiceEntity.getId() + "' order by create_Date desc");
                if (dateList.size() != 0) {
                    request.setAttribute("dateList", dateList.get(0));
                }
            }
            //上门次数
            List<Object[]> arrivalofabnormalEntities = systemService.findListbySql("select * from arrival_of_abnormal where service_id = '" + id + "' ORDER BY create_date DESC  LIMIT 1;");
            if (arrivalofabnormalEntities != null && arrivalofabnormalEntities.size() > 0) {
                Integer num = (Integer) arrivalofabnormalEntities.get(0)[2];
                request.setAttribute("signNumbers", "" + num + "次");
            } else {
                request.setAttribute("signNumbers", "无");
            }
            /*获取编辑页面基本信息*/
            List<Map<String, Object>> mapList = cbsServiceServiceI.getCbsServiceByCid(cbsServiceEntity.getId());
            request.setAttribute("mapList", mapList.get(0));
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);
            List<TSTerritory> shiList = systemService.findHql("from TSTerritory where territoryLevel = 2 order by territorySort");
            request.setAttribute("shiList", shiList);
            List<CbsEngineSceneCheckReportEntity> cbsEngineSceneCheckReportHql = systemService.findHql("from CbsEngineSceneCheckReportEntity where serviceId='" + id + "'");
            request.setAttribute("cbsEngineSceneCheckReportHql", cbsEngineSceneCheckReportHql.get(0));
            List<ZyFileLogEntity> zyFileLogEntityG1Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='G1'");
            request.setAttribute("zyFileLogEntityG1Hql", zyFileLogEntityG1Hql);
            List<ZyFileLogEntity> zyFileLogEntityG2Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='G2'");
            request.setAttribute("zyFileLogEntityG2Hql", zyFileLogEntityG2Hql);
            List<ZyFileLogEntity> zyFileLogEntityG3Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='G3'");
            request.setAttribute("zyFileLogEntityG3Hql", zyFileLogEntityG3Hql);
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/engine_refurbished");
    }

    /**
     * 引擎翻新现场检测完工单详情
     */
    @RequestMapping(params = "goEngineRefurbishedDetail")
    public ModelAndView goEngineRefurbishedDetail(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.get(CbsServiceEntity.class, id);
            if (cbsServiceEntity != null) {
                request.setAttribute("cbsServiceEntity", cbsServiceEntity);
                List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsServiceEntity.getId() + "' order by create_Date desc");
                if (dateList.size() != 0) {
                    request.setAttribute("dateList", dateList.get(0));
                }
            }
            /*获取编辑页面基本信息*/
            List<Map<String, Object>> mapList = cbsServiceServiceI.getCbsServiceByCid(cbsServiceEntity.getId());
            request.setAttribute("mapList", mapList.get(0));
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);
            List<TSTerritory> shiList = systemService.findHql("from TSTerritory where territoryLevel = 2 order by territorySort");
            request.setAttribute("shiList", shiList);
            List<CbsEngineSceneCheckReportEntity> cbsEngineSceneCheckReportHql = systemService.findHql("from CbsEngineSceneCheckReportEntity where serviceId='" + id + "'");
            request.setAttribute("cbsEngineSceneCheckReportHql", cbsEngineSceneCheckReportHql.get(0));
            if (cbsEngineSceneCheckReportHql.size() != 0 && StringUtil.isNotEmpty(cbsEngineSceneCheckReportHql.get(0).getEquipmentType())) {
                ZyEquipmentTypeEntity zyEquipmentType = systemService.getEntity(ZyEquipmentTypeEntity.class, cbsEngineSceneCheckReportHql.get(0).getEquipmentType());
                request.setAttribute("zyEquipmentType", zyEquipmentType);
            }
            List<ZyFileLogEntity> zyFileLogEntityG1Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + cbsServiceEntity.getId() + "' and businessType='G1'");
            request.setAttribute("zyFileLogEntityG1Hql", zyFileLogEntityG1Hql);
            List<ZyFileLogEntity> zyFileLogEntityG2Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + cbsServiceEntity.getId() + "' and businessType='G2'");
            request.setAttribute("zyFileLogEntityG2Hql", zyFileLogEntityG2Hql);
            List<ZyFileLogEntity> zyFileLogEntityG3Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + cbsServiceEntity.getId() + "' and businessType='G3'");
            request.setAttribute("zyFileLogEntityG3Hql", zyFileLogEntityG3Hql);
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/engine_refurbished_detail");
    }

    /**
     * 引擎翻新现场检测完工单暂存和提交
     */
    @RequestMapping(params = "addEngineRefurbished")
    @ResponseBody
    public AjaxJson addEngineRefurbished(HttpServletRequest request, CbsEngineSceneCheckReportEntity cbsEngineSceneCheckReport) throws Exception {
        AjaxJson j = new AjaxJson();
        try {
            j = cbsServiceReportService.saveReport(request, cbsEngineSceneCheckReport);
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setSuccess(false);
            e.printStackTrace();
        }
        return j;
    }

    /**
     * 现场检测完工单暂存和提交
     */
    @RequestMapping(params = "addSceneCheckReport")
    @ResponseBody
    public AjaxJson addSceneCheckReport(HttpServletRequest request, CbsSceneCheckReportEntity cbsSceneCheckReport) {
        AjaxJson j = new AjaxJson();
        j = cbsServiceReportService.addSceneCheckReport(request,cbsSceneCheckReport);
        return j;
    }

    /**
     * 现场检测完工单
     */
    @RequestMapping(params = "goReferDetection")
    public ModelAndView goReferDetection(HttpServletRequest request) {
        //是否为补填完工单
        Boolean editReport = Boolean.valueOf(oConvertUtils.getString(request.getParameter("editReport")));
        request.setAttribute("editReport", editReport);

        String type = request.getParameter("type");
        request.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "");
        String id = request.getParameter("id");
        try {
            CbsServiceEntity cbsServiceEntity = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            if (cbsServiceEntity != null) {
                List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsServiceEntity.getId() + "' order by create_Date desc");
                if (dateList.size() != 0) {
                    request.setAttribute("dateList", dateList.get(0));
                }
                request.setAttribute("cbsServiceEntity", cbsServiceEntity);

                //获取派单要求
                Map<String, Object> assMap = systemService.findOneForJdbc("SELECT * FROM `cbs_service_assign_list` WHERE service_id='" + id + "' GROUP BY create_date DESC LIMIT 1");
                request.setAttribute("serviceAssign", assMap);
            }
            //上门次数
            List<Object[]> arrivalofabnormalEntities = systemService.findListbySql("select * from arrival_of_abnormal where service_id = '" + id + "' ORDER BY create_date DESC  LIMIT 1;");
            if (arrivalofabnormalEntities != null && arrivalofabnormalEntities.size() > 0) {
                Integer num = (Integer) arrivalofabnormalEntities.get(0)[2];
                request.setAttribute("signNumbers", "" + num + "次");
            } else {
                request.setAttribute("signNumbers", "无");
            }
            /*获取编辑页面基本信息*/
            List<Map<String, Object>> mapList = cbsServiceServiceI.getCbsServiceByCid(id);
            request.setAttribute("mapList", mapList.get(0));
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);
            List<TSTerritory> shiList = systemService.findHql("from TSTerritory where territoryLevel = 2 order by territorySort");
            request.setAttribute("shiList", shiList);
            List<CbsSceneCheckReportEntity> cbsSceneCheckReportHql = systemService.findHql("from CbsSceneCheckReportEntity where serviceId='" + id + "'");
            request.setAttribute("cbsSceneCheckReportHql", cbsSceneCheckReportHql.get(0));
            List<ZyFileLogEntity> zyFileLogEntityF1Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F1'");
            request.setAttribute("zyFileLogEntityF1Hql", zyFileLogEntityF1Hql);
            List<ZyFileLogEntity> zyFileLogEntityF2Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F2'");
            request.setAttribute("zyFileLogEntityF2Hql", zyFileLogEntityF2Hql);
            List<ZyFileLogEntity> zyFileLogEntityF3Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F3'");
            request.setAttribute("zyFileLogEntityF3Hql", zyFileLogEntityF3Hql);
            List<ZyFileLogEntity> zyFileLogEntityF4Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F4'");
            request.setAttribute("zyFileLogEntityF4Hql", zyFileLogEntityF4Hql);
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/refer_detection");
    }

    /**
     * 查看现场检测完工单详情
     */
    @RequestMapping(params = "goReferDetectionDetail")
    public ModelAndView goReferDetectionDetail(HttpServletRequest request) {
        String id = request.getParameter("id");
        try {
            CbsServiceEntity cbsServiceEntity = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            if (cbsServiceEntity != null) {
                List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsServiceEntity.getId() + "' order by create_Date desc");
                if (dateList.size() != 0) {
                    request.setAttribute("dateList", dateList.get(0));
                }
                request.setAttribute("cbsServiceEntity", cbsServiceEntity);
            }
            /*获取编辑页面基本信息*/
            List<Map<String, Object>> mapList = cbsServiceServiceI.getCbsServiceByCid(id);
            request.setAttribute("mapList", mapList.get(0));
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);
            List<TSTerritory> shiList = systemService.findHql("from TSTerritory where territoryLevel = 2 order by territorySort");
            request.setAttribute("shiList", shiList);
            List<CbsSceneCheckReportEntity> cbsSceneCheckReportHql = systemService.findHql("from CbsSceneCheckReportEntity where serviceId='" + id + "'");
            request.setAttribute("cbsSceneCheckReportHql", cbsSceneCheckReportHql.get(0));
            if (cbsSceneCheckReportHql.size() != 0 && StringUtil.isNotEmpty(cbsSceneCheckReportHql.get(0).getEquipmentType())) {
                ZyEquipmentTypeEntity zyEquipmentType = systemService.getEntity(ZyEquipmentTypeEntity.class, cbsSceneCheckReportHql.get(0).getEquipmentType());
                request.setAttribute("zyEquipmentType", zyEquipmentType);
            }
            List<ZyFileLogEntity> zyFileLogEntityF1Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F1'");
            request.setAttribute("zyFileLogEntityF1Hql", zyFileLogEntityF1Hql);
            List<ZyFileLogEntity> zyFileLogEntityF2Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F2'");
            request.setAttribute("zyFileLogEntityF2Hql", zyFileLogEntityF2Hql);
            List<ZyFileLogEntity> zyFileLogEntityF3Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F3'");
            request.setAttribute("zyFileLogEntityF3Hql", zyFileLogEntityF3Hql);
            List<ZyFileLogEntity> zyFileLogEntityF4Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='F4'");
            request.setAttribute("zyFileLogEntityF4Hql", zyFileLogEntityF4Hql);
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/refer_detection_detail");
    }

    /**
     * 光管翻新验收单
     */
    @RequestMapping(params = "goLighttubeAcceptance")
    public ModelAndView goLighttubeAcceptance(HttpServletRequest request) {
        TSUser user = ResourceUtil.getSessionUserName();

        //用户的角色集合
        List<String> roleList = new ArrayList<>();
        List<TSRoleUser> tsRoleUserList = systemService.findHql("from TSRoleUser where TSUser.id='" + user.getId() + "'");
        for (TSRoleUser tsRoleUser : tsRoleUserList) {
            String role = tsRoleUser.getTSRole().getRoleCode();
            roleList.add(role);
        }
        if (roleList.contains("barcoHelpdesk")) {
            request.setAttribute("typeRole", "ser");
        }
        if (roleList.contains("admin")) {
            request.setAttribute("typeRole", "ser");
        }
        try {
            //是否为补填完工单
            Boolean editReport = Boolean.valueOf(oConvertUtils.getString(request.getParameter("editReport")));
            request.setAttribute("editReport", editReport);

            String type = request.getParameter("type");
            request.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.get(CbsServiceEntity.class, id);
            //获取已暂存光管翻新验收单
            CbsLightReportEntity lightReport = systemService.singleResult("from CbsLightReportEntity where serviceId.id='" + id + "'");
            if (lightReport != null) {
                request.setAttribute("lightReport", lightReport);
                if (lightReport.getSheng() != null) {
                    List<TSTerritory> cityList = systemService.findHql("from TSTerritory where territoryLevel = 2 and TSTerritory.territoryCode = '" + lightReport.getSheng().getTerritoryCode() + "' ");
                    request.setAttribute("cityList", cityList);
                }

                //获取照片
                List<ZyFileLogEntity> photoFileList = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='I' and businessChildType='IP'");
                request.setAttribute("photoFileList", photoFileList);
                //获取现场完工附件
                List<ZyFileLogEntity> finishFileList = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='I' and businessChildType='IF'");
                request.setAttribute("finishFileList", finishFileList);
            } else {
                if (null != cbsServiceEntity) {
                    request.setAttribute("cbsServiceEntity", cbsServiceEntity);
                    if (cbsServiceEntity.getProvince() != null) {
                        List<TSTerritory> cityList = systemService.findHql("from TSTerritory where territoryLevel = 2 and TSTerritory.territoryCode = '" + cbsServiceEntity.getProvince().getTerritoryCode() + "' ");
                        request.setAttribute("cityList", cityList);
                    }
                    //获取上门次数和实际服务时间
                    String sql = "SELECT * FROM `arrival_of_abnormal` WHERE service_id='" + id + "' GROUP BY create_date DESC LIMIT 1;";
                    Map<String, Object> map = systemService.findOneForJdbc(sql);
                    request.setAttribute("arrivalofabnormal", map);
                    //ArrivalofabnormalEntity afe=systemService.singleResult("");
                }
            }


            //获取所有省份
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);

            //获取所有照片类型
            List<TSType> photoTypes = systemService.findHql("from TSType where TSTypegroup.typegroupcode='photoType'");
            request.setAttribute("photoTypes", photoTypes);

            //查询所有放映机型号
            //List<ZyEquipmentTypeEntity> equipmentTypes=systemService.findHql("from ZyEquipmentTypeEntity where ");
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/lighttube_acceptance");

    }

    /**
     * 查看光管翻新验收单详情
     */
    @RequestMapping(params = "goLighttubeAcceptanceDetail")
    public ModelAndView goLighttubeAcceptanceDetail(HttpServletRequest request) {
        String id = request.getParameter("id");
        String fail = "该服务单还没有填写完工单";
        try {
            //获取已暂存光管翻新验收单
            CbsLightReportEntity lightReport = systemService.singleResult("from CbsLightReportEntity where serviceId.id='" + id + "'");
            if (lightReport != null) {
                request.setAttribute("lightReport", lightReport);
                //获取照片类型
                TSType photoTypes = systemService.singleResult("from TSType where TSTypegroup.typegroupcode='photoType' and typecode='" + lightReport.getPhotoType() + "'");
                request.setAttribute("photoTypes", photoTypes);

                //获取照片
                List<ZyFileLogEntity> photoFileList = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='I' and businessChildType='IP'");
                request.setAttribute("photoFileList", photoFileList);
                //获取现场完工附件
                List<ZyFileLogEntity> finishFileList = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='I' and businessChildType='IF'");
                request.setAttribute("finishFileList", finishFileList);

            } else {
                request.setAttribute("fail", fail);
            }

        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/lighttube_acceptance_detail");

    }

    /**
     * 技术支持完工单
     */
    @RequestMapping(params = "goTechnologyAgree")
    public ModelAndView goTechnologyAgree(HttpServletRequest request) {

        TSUser user = ResourceUtil.getSessionUserName();

        //用户的角色集合
        List<String> roleList = new ArrayList<>();
        List<TSRoleUser> tsRoleUserList = systemService.findHql("from TSRoleUser where TSUser.id='" + user.getId() + "'");
        for (TSRoleUser tsRoleUser : tsRoleUserList) {
            String role = tsRoleUser.getTSRole().getRoleCode();
            roleList.add(role);
        }
        if (roleList.contains("barcoHelpdesk")) {
            request.setAttribute("typeRole", "ser");
        }
        if (roleList.contains("admin")) {
            request.setAttribute("typeRole", "ser");
        }

        try {
            //是否为补填完工单
            Boolean editReport = Boolean.valueOf(oConvertUtils.getString(request.getParameter("editReport")));
            request.setAttribute("editReport", editReport);

            String type = request.getParameter("type");
            request.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "");
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            CbsTechologyAgreeReportEntity ta = systemService.singleResult("from CbsTechologyAgreeReportEntity where serviceId.id='" + id + "'");

            if (ta != null) {
                request.setAttribute("techologyAgree", ta);
                if (ta.getSheng() != null) {
                    String hql = "from TSTerritory where territoryLevel = 2 and TSTerritory.territoryCode = '" + ta.getSheng().getTerritoryCode() + "' ";
                    List<TSTerritory> cityList = systemService.findHql(hql);
                    request.setAttribute("cityList", cityList);
                }

                //获取附件照片
                List<ZyFileLogEntity> fileList = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='J'");
                request.setAttribute("fileList", fileList);
            } else {
                if (null != cbsServiceEntity) {
                    request.setAttribute("cbsServiceEntity", cbsServiceEntity);
                    if (cbsServiceEntity.getProvince() != null) {
                        String hql = "from TSTerritory where territoryLevel = 2 and TSTerritory.territoryCode = '" + cbsServiceEntity.getProvince().getTerritoryCode() + "' ";
                        List<TSTerritory> cityList = systemService.findHql(hql);
                        request.setAttribute("cityList", cityList);
                    }
                    //获取上门次数和实际服务时间
                    String sql = "SELECT * FROM `arrival_of_abnormal` WHERE service_id='" + id + "' GROUP BY create_date DESC LIMIT 1;";
                    Map<String, Object> map = systemService.findOneForJdbc(sql);
                    request.setAttribute("arrivalofabnormal", map);

                    //获取派单要求
                    Map<String, Object> assMap = systemService.findOneForJdbc("SELECT * FROM `cbs_service_assign_list` WHERE service_id='" + id + "' GROUP BY create_date DESC LIMIT 1");
                    request.setAttribute("serviceAssign", assMap);
                }
            }
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);


        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/technology_agree");
    }

    /**
     * 查看技术支持完工单详情
     */
    @RequestMapping(params = "goTechnologyAgreeDetail")
    public ModelAndView goTechnologyAgreeDetail(HttpServletRequest request) {
        String fail = "该服务单还没有填写完工单";
        String id = request.getParameter("id");
        try {
            CbsServiceEntity cbsServiceEntity = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            CbsTechologyAgreeReportEntity ta = systemService.singleResult("from CbsTechologyAgreeReportEntity where serviceId.id='" + id + "'");
            if (ta != null) {
                request.setAttribute("techologyAgree", ta);
                //获取附件照片
                List<ZyFileLogEntity> fileList = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='J'");
                request.setAttribute("fileList", fileList);
            } else {
                request.setAttribute("fail", fail);
            }

        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/technology_agree_detail");
    }

    /**
     * 引擎翻新验收单
     */
    @RequestMapping(params = "goEngineRefurbishedSign")
    public ModelAndView goEngineRefurbishedSign(HttpServletRequest request) {
        try {
            //是否为 补填完工单
            Boolean editReport = Boolean.valueOf(oConvertUtils.getString(request.getParameter("editReport")));
            request.setAttribute("editReport", editReport);

            String type = request.getParameter("type");
            request.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            if (null != cbsServiceEntity) {
                List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsServiceEntity.getId() + "' order by create_Date desc");
                if (dateList.size() != 0) {
                    request.setAttribute("dateList", dateList.get(0));
                }
                request.setAttribute("cbsServiceEntity", cbsServiceEntity);
            }
            //上门次数
            List<Object[]> arrivalofabnormalEntities = systemService.findListbySql("select * from arrival_of_abnormal where service_id = '" + id + "' ORDER BY create_date DESC  LIMIT 1;");
            if (arrivalofabnormalEntities != null && arrivalofabnormalEntities.size() > 0) {
                Integer num = (Integer) arrivalofabnormalEntities.get(0)[2];
                request.setAttribute("signNumbers", "" + num + "次");
            } else {
                request.setAttribute("signNumbers", "无");
            }
            /*获取编辑页面基本信息*/
            List<Map<String, Object>> mapList = cbsServiceServiceI.getCbsServiceByCid(id);
            request.setAttribute("mapList", mapList.get(0));
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);
            List<TSTerritory> shiList = systemService.findHql("from TSTerritory where territoryLevel = 2 order by territorySort");
            request.setAttribute("shiList", shiList);
            List<CbsEngineAcceptEntity> cbsEngineAcceptReportHql = systemService.findHql("from CbsEngineAcceptEntity where serviceId='" + id + "'");
            request.setAttribute("cbsEngineAcceptReportHql", cbsEngineAcceptReportHql.get(0));
            List<ZyFileLogEntity> zyFileLogEntityH1Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='H1'");
            request.setAttribute("zyFileLogEntityH1Hql", zyFileLogEntityH1Hql);
            List<ZyFileLogEntity> zyFileLogEntityH2Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='H2'");
            request.setAttribute("zyFileLogEntityH2Hql", zyFileLogEntityH2Hql);
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/engine_refurbished_sign");
    }

    /**
     * 查看引擎翻新验收单详情
     */
    @RequestMapping(params = "goEngineRefurbishedSignDetail")
    public ModelAndView goEngineRefurbishedSignDetail(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            if (null != cbsServiceEntity) {
                List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsServiceEntity.getId() + "' order by create_Date desc");
                if (dateList.size() != 0) {
                    request.setAttribute("dateList", dateList.get(0));
                }
                request.setAttribute("cbsServiceEntity", cbsServiceEntity);
            }
            /*获取编辑页面基本信息*/
            List<Map<String, Object>> mapList = cbsServiceServiceI.getCbsServiceByCid(id);
            request.setAttribute("mapList", mapList.get(0));
            List<TSTerritory> territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");
            request.setAttribute("territoryList", territoryList);
            List<TSTerritory> shiList = systemService.findHql("from TSTerritory where territoryLevel = 2 order by territorySort");
            request.setAttribute("shiList", shiList);
            List<CbsEngineAcceptEntity> cbsEngineAcceptReportHql = systemService.findHql("from CbsEngineAcceptEntity where serviceId='" + id + "'");
            request.setAttribute("cbsEngineAcceptReportHql", cbsEngineAcceptReportHql.get(0));
            if (cbsEngineAcceptReportHql.size() != 0 && StringUtil.isNotEmpty(cbsEngineAcceptReportHql.get(0).getEquipmentType())) {
                ZyEquipmentTypeEntity zyEquipmentType = systemService.getEntity(ZyEquipmentTypeEntity.class, cbsEngineAcceptReportHql.get(0).getEquipmentType());
                request.setAttribute("zyEquipmentType", zyEquipmentType);
            }
            List<ZyFileLogEntity> zyFileLogEntityH1Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='H1'");
            request.setAttribute("zyFileLogEntityH1Hql", zyFileLogEntityH1Hql);
            List<ZyFileLogEntity> zyFileLogEntityH2Hql = systemService.findHql("from ZyFileLogEntity where businessKey='" + id + "' and businessType='H2'");
            request.setAttribute("zyFileLogEntityH2Hql", zyFileLogEntityH2Hql);
        } catch (Exception e) {
            e.getMessage();
        }
        return new ModelAndView("com/zybk/cbsServiceReport/engine_refurbished_sign_detail");
    }

    /**
     * 引擎翻新验收单的暂存和提交
     */
    @RequestMapping(params = "addRefurbishedSign")
    @ResponseBody
    public AjaxJson addRefurbishedSign(HttpServletRequest request, CbsEngineAcceptEntity cbsEngineAccept) {
        AjaxJson j = new AjaxJson();
        try {
            j = cbsServiceReportService.saveYqReport(request, cbsEngineAccept);
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setSuccess(false);
            e.printStackTrace();
        }
        return j;
    }

    /**
     * 现场服务完工报告列表 页面跳转
     *
     * @return
     */
    @RequestMapping(params = "cbsServiceReport")
    public ModelAndView cbsServiceReport(HttpServletRequest request) {
        return new ModelAndView("com/zybk/cbsServiceReport/cbsServiceReportList");
    }

    @RequestMapping(params = "getUuid")
    @ResponseBody
    public String getUuid() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid;
    }

    /**
     * 获取备件类型
     *
     * @return
     */
    @RequestMapping(params = "getType")
    @ResponseBody
    public AjaxJson getType() {
        AjaxJson ajaxJson = new AjaxJson();
        try {
//            List<Map<String, Object>> maps = systemService.findForJdbc("SELECT id,type_desc FROM zy_equipment_type WHERE parent_id in (select id from zy_equipment_type where type_desc= '放映机备件')");
            //8月29号，修改bug 8730：取备件号是序列号追踪的，然后型号去重显示在完工单备件更换部分的新备件类型里面
            List<Map<String, Object>> maps = systemService.findForJdbc("SELECT t.id, t.type_desc FROM zy_equipment_type t LEFT JOIN zy_es_item i ON t.id = i.type WHERE i.is_track = \"1\" AND t.parent_id IN ( SELECT id FROM zy_equipment_type WHERE type_desc = '放映机备件' ) GROUP BY t.type_desc");
            Object id = maps.get(0).get("type_desc");
            ajaxJson.setObj(maps);
        } catch (Exception e) {
            e.getMessage();
        }
        return ajaxJson;
    }

    /**
     * 获取备件号
     *
     * @return
     */
    @RequestMapping(params = "getItem")
    @ResponseBody
    public AjaxJson getItem(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        String id = request.getParameter("id");
        try {
            List<Map<String, Object>> maps = systemService.findForJdbc("select id,item from zy_es_item where type = '" + id + "'");
            if (maps != null && maps.size() > 0) {
                ajaxJson.setObj(maps);
            } else {
                ajaxJson.setObj(null);
            }

        } catch (Exception e) {
            e.getMessage();
        }
        return ajaxJson;
    }

    /**
     * easyui AJAX请求数据
     *
     * @param request
     * @param response
     * @param dataGrid
     * @param
     */

    @RequestMapping(params = "datagrid")
    public void datagrid(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
        CriteriaQuery cq = new CriteriaQuery(CbsServiceReportEntity.class, dataGrid);
        //查询条件组装器
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cbsServiceReport, request.getParameterMap());
        try {
            //自定义追加查询条件
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        }
        cq.add();
        this.cbsServiceReportService.getDataGridReturn(cq, true);
        TagUtil.datagrid(response, dataGrid);
    }

    /**
     * 现场服务完工报告新增页面跳转
     *
     * @return
     */
    @RequestMapping(params = "goAdd")
    public ModelAndView goAdd(CbsServiceReportEntity cbsServiceReport, HttpServletRequest req) {
        if (StringUtil.isNotEmpty(cbsServiceReport.getId())) {
            cbsServiceReport = cbsServiceReportService.getEntity(CbsServiceReportEntity.class, cbsServiceReport.getId());
            req.setAttribute("cbsServiceReportPage", cbsServiceReport);
        }
        return new ModelAndView("com/zybk/report/cbsServiceReport-add");
    }


    /**
     * 添加现场服务完工报告
     *
     * @param
     * @return
     */
    @RequestMapping(params = "doAdd")
    @ResponseBody
    public AjaxJson doAdd(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        j = cbsServiceReportService.doAdd(cbsServiceReport, request);
        return j;
    }

    /**
     * 改变文件名字
     */
    @ResponseBody
    @RequestMapping(params = "changeName")
    public AjaxJson changeName(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        if (StringUtil.isNotEmpty(id)) {
            systemService.updateBySqlString("UPDATE zy_file_log set old_file_name = '" + name + "' where id  = '" + id + "'");
            ajaxJson.setSuccess(true);
        }
        return ajaxJson;
    }

    /**
     * 比较日期
     *
     * @param dt1
     * @param dt2
     * @return
     */
    public static int CompareDate(Date dt1, Date dt2) {
        try {
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 上传附件功能
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "fileUploadView")
    public AjaxJson fileUploadView(HttpServletRequest request) throws UnsupportedEncodingException {
        AjaxJson j = new AjaxJson();
        Map<String, Object> attributes = new HashMap<String, Object>();
        String cbsServiceId = oConvertUtils.getString(request.getParameter("cbsServiceId"));
        String businessType = request.getParameter("businessType");
        String typeS = request.getParameter("type");
        String type = URLDecoder.decode(typeS, "UTF-8");
        if (cbsServiceId != null && !cbsServiceId.equals("")) {
            FileBean fileBean = UploadFileUtil.UploadFile(request);
            if (StringUtil.isNotEmpty(cbsServiceId)) {
                ZyFileLogEntity zyfileLog = new ZyFileLogEntity();
                zyfileLog.setBusinessKey(cbsServiceId);
                zyfileLog.setFilePath(fileBean.getSavePath());
                zyfileLog.setOldFileName(fileBean.getFileRealName());
                zyfileLog.setNewFileName(fileBean.getFileName());
//                zyfileLog.setCreateDate(new Date());
                zyfileLog.setFileType("wgd");
                if ("F".equals(businessType)) {
                    zyfileLog.setBusinessType("F");

                    systemService.save(zyfileLog);
                } else {

                    systemService.save(zyfileLog);
                }
//
            }
            String id = null;
            Map<String, Object> fileLogMap = systemService.findOneForJdbc("SELECT t.id FROM zy_file_log t where  t.business_key = '" + cbsServiceId + "' and old_file_name = '" + fileBean.getFileRealName() + "'order by t.create_date desc limit 1 ");
            if (fileLogMap != null) {
                id = fileLogMap.get("id").toString();
            }
            Map<String, Object> map = new HashMap<>();
            try {
//                if (fileBean.getFileRealName().indexOf("ICP")!=-1){
                Map<String, Object> map3 = readZipFile(fileBean.getFilePath(), "Version_info.txt", fileBean.getFileRealName());
//                }
//                if (fileBean.getFileRealName().indexOf("ICMP")!=-1){
                Map<String, Object> map1 = readZipFile(fileBean.getFilePath(), "ICMPVersionInfo.xml", fileBean.getFileRealName());
                Map<String, Object> map2 = readZipFile(fileBean.getFilePath(), "Parameters.txt", fileBean.getFileRealName());
                Object lampTime = map3.get("lampTime");
                Object barcoVs = map3.get("barcoVs");
                Object tiVs = map3.get("tiVs");
                Object ICMPvs = map3.get("ICMPvs");
                Object lensVs = map3.get("lensVs");
                Object serviceXh = map3.get("serviceXh");
                Object projectorTime = map3.get("projectorTime");
                Object serialNumber = map2.get("serialNumber");
                if (serialNumber != null) {
                    CbsServiceEntity cbsServiceEntity = systemService.getEntity(CbsServiceEntity.class, cbsServiceId);
                    if (cbsServiceEntity.getSn().equals(serialNumber)) {
                        map.put("ststus", 0);
                    }
                    if (!cbsServiceEntity.getSn().equals(serialNumber)) {
                        map.put("ststus", 1);
                    }
                }
                if (lampTime != "" && lampTime != null) {
                    map.put("lampTime", lampTime);
                }
                if (barcoVs != "") {
                    map.put("barcoVs", barcoVs);
                }
                if (tiVs != "" && tiVs != null) {
                    map.put("tiVs", tiVs);
                }
                if (lensVs != "" && lensVs != null) {
                    map.put("lensVs", lensVs);
                }
                if (ICMPvs != "" && ICMPvs != null) {
                    map.put("ICMPvs", ICMPvs);
                }
                if (serviceXh != "" && serviceXh != null) {
                    map.put("serviceXh", serviceXh);
                }
                if (projectorTime != "" && projectorTime != null) {
                    map.put("projectorTime", projectorTime);
                }
                Object lampTime1 = map1.get("lampTime");
                Object barcoVs1 = map1.get("barcoVs");
                Object tiVs1 = map1.get("tiVs");
                Object ICMPvs1 = map1.get("ICMPvs");
                Object lensVs1 = map1.get("lensVs");
                Object serviceXh1 = map1.get("serviceXh");
                Object projectorTime1 = map1.get("projectorTime");
                if (lampTime1 != null && lampTime1 != "") {
                    map.put("lampTime", lampTime1);
                }
                if (barcoVs1 != null && barcoVs1 != "") {
                    map.put("barcoVs", barcoVs1);
                }
                if (tiVs1 != null && tiVs1 != "") {
                    map.put("tiVs", tiVs1);
                }
                if (lensVs1 != null && lensVs1 != "") {
                    map.put("lensVs", lensVs1);
                }
                if (ICMPvs1 != null && ICMPvs1 != "") {
                    map.put("ICMPvs", ICMPvs1);
                }
                if (serviceXh1 != null && serviceXh1 != "") {
                    map.put("serviceXh", serviceXh1);
                }
                if (projectorTime1 != null && projectorTime1 != "") {
                    map.put("projectorTime", projectorTime1);
                }
                Object lampTime2 = map2.get("lampTime");
                Object barcoVs2 = map2.get("barcoVs");
                Object tiVs2 = map2.get("tiVs");
                Object ICMPvs2 = map2.get("ICMPvs");
                Object lensVs2 = map2.get("lensVs");
                Object serviceXh2 = map2.get("serviceXh");
                Object projectorTime2 = map2.get("projectorTime");
                if (lampTime2 != "" && lampTime2 != null) {
                    map.put("lampTime", lampTime2);
                }
                if (barcoVs2 != "" && barcoVs2 != null) {
                    map.put("barcoVs", barcoVs2);
                }
                if (tiVs2 != "" && tiVs2 != null) {
                    map.put("tiVs", tiVs2);
                }
                if (lensVs2 != "" && lensVs2 != null) {
                    map.put("lensVs", lensVs2);
                }
                if (ICMPvs2 != "" && ICMPvs2 != null) {
                    map.put("ICMPvs", ICMPvs2);
                }
                if (serviceXh2 != "" && serviceXh2 != null) {
                    map.put("serviceXh", serviceXh2);
                }
                if (projectorTime2 != "" && projectorTime2 != null) {
                    map.put("projectorTime", projectorTime2);
                }
//                    attributes.putAll(map1);
//                    attributes.putAll(map2);
                attributes.putAll(map);
//                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            attributes.put("name", fileBean.getFileRealName());
            attributes.put("id", id);
            attributes.put("type", type);
            j.setAttributes(attributes);
        }
        return j;
    }

    /**
     * 解析zip中的内容
     *
     * @param file
     * @throws Exception
     */
    public static Map<String, Object> readZipFile(String file, String fileType, String fileName) throws Exception {
        Map<String, Object> map = new HashMap<>();
        ZipFile zf = new ZipFile(file);
        Charset gbk = Charset.forName("gbk");
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in, gbk);
        ZipEntry ze;
        String html = "";
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
            } else if (ze.getName().equals(fileType)) {
                System.err.println("file - " + ze.getName() + " : "
                        + ze.getSize() + " bytes");
                long size = ze.getSize();
                if (size > 0) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(zf.getInputStream(ze)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        html += line;
                    }
//                            System.out.println(html);
                    br.close();
                }
                if (html != "") {
                    Document doc = Jsoup.parse(html);
                    //根据文件名称判断诊断包类型
//                if (fileName.indexOf("ICP")!=-1){
                    Elements selectI = doc.select("package[name=\"barco\"]");
                    String vs = selectI.attr("vs");
                    Elements selectC = doc.select("package[name=\"ti\"]");
                    String vs1 = selectC.attr("vs");
                    map.put("barcoVs", vs);
                    map.put("tiVs", vs1);
//                }
//                if (fileName.indexOf("ICMP")!=-1){
                    //ICMP版本
                    Elements select = doc.select("package[name=\"barco icmp package\"]");
                    String ICMPvs = select.attr("vs");
                    if (ICMPvs != "" && ICMPvs != null) {
                        String substring = ICMPvs.substring(0, ICMPvs.lastIndexOf("."));
                        map.put("ICMPvs", substring);
                    } else {
                        map.put("ICMPvs", ICMPvs);
                    }

                    //镜头型号示例
                    Elements select1 = doc.select("Lens");
                    String lensVs = select1.attr("description");
                    map.put("lensVs", lensVs);
                    //机型
                    Elements select3 = doc.select("Projector ");
                    String typeDesc = select3.attr("Identifier");
                    //序列号
                    Elements select4 = doc.select("Projector ");
                    String serialNumber = select4.attr("serial_number");
                    map.put("serialNumber", serialNumber);
                    //IMB型号
                    Elements select2 = doc.select("Mediablock ");
                    String vs2 = select2.attr("type");
                    if (vs2.equals("BARCO ICMP")) {
                        map.put("serviceXh", "Barco");
                    } else if (vs2.equals("GDC_KXHXwngJjNuk")) {
                        map.put("serviceXh", "GDC");
                    } else if (vs2.equals("Doremi IMB")) {
                        map.put("serviceXh", "Doremi");
                    } else if (vs2.equals("usl_mbu100")) {
                        map.put("serviceXh", "Dobly");
                    } else if (vs2.equals("MikroM_Qube Xi")) {
                        map.put("serviceXh", "大地辰星");
                    } else {
                        map.put("serviceXh", "其他");
                    }
                    Elements Lamp = doc.select("Lamp");
                    String lampTime = Lamp.attr("runtime");
                    map.put("lampTime", lampTime);
                    Elements Projector = doc.select("Projector");
                    String projectorTime = Projector.attr("runtime");
                    map.put("projectorTime", projectorTime);
                }

//                }
            }
            zin.closeEntry();
        }
        return map;

    }

    //工程点击填写完工报告直接跳转暂存的相应界面
    @ResponseBody
    @RequestMapping(params = "goTemporaryPage")
    public AjaxJson goTemporaryPage(HttpServletRequest request) {
        AjaxJson a = new AjaxJson();
        Map<String, Object> map = new HashMap();
        try {
            String id = request.getParameter("id");
            CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id='" + id + "' and status = 0");
            if (cbsServiceReportEntity != null) {
                if (cbsServiceReportEntity.getReportCategory() == 1) {
                    map.put("reportCategory", 1);
                }
                if (cbsServiceReportEntity.getReportCategory() == 2) {
                    map.put("reportCategory", 2);
                }
            } else {
                map.put("reportCategory", -1);
            }
            a.setAttributes(map);
        } catch (Exception e) {
            e.getMessage();
        }
        return a;
    }

    //工程点击填写完工报告直接跳转暂存的相应界面
    @ResponseBody
    @RequestMapping(params = "goTemporaryPageOnSubmit")
    public AjaxJson goTemporaryPageOnSubmit(HttpServletRequest request) {
        AjaxJson a = new AjaxJson();
        Map map = new HashMap();
        try {
            String id = request.getParameter("id");
            CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id='" + id + "' and status = 1");
            if (cbsServiceReportEntity != null) {
                map.put("reportCategory", 1);
            } else {
                map.put("reportCategory", -1);
            }
            a.setAttributes(map);
        } catch (Exception e) {
            e.getMessage();
        }
        return a;

    }

    /**
     * @Author X On 2018/10/11 15:48
     * @Description 跳转故障维修对应的完工单编辑页
     */
    @ResponseBody
    @RequestMapping(params = "goCbsServiceRepairReport")
    public AjaxJson goCbsServiceRepairReport(HttpServletRequest request) {
        AjaxJson a = new AjaxJson();
        Map<String, Object> m = new HashMap();
        try {
            String id = request.getParameter("id");
            CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id='" + id + "' ");
            if (cbsServiceReportEntity != null) {
                if (cbsServiceReportEntity.getReportCategory() == 1) {
                    m.put("reportCategory", 1);
                }
                if (cbsServiceReportEntity.getReportCategory() == 2) {
                    m.put("reportCategory", 2);
                }
            } else {
                m.put("reportCategory", -1);
            }
            a.setAttributes(m);
        } catch (Exception e) {
            e.getMessage();
        }
        return a;
    }

    /**
     * 添加现场服务完工报告
     *
     * @param
     * @return
     */
    @RequestMapping(params = "zanAdd")
    @ResponseBody
    public AjaxJson zanAdd(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        j = cbsServiceReportService.zanAdd(cbsServiceReport, request);
        return j;
    }

    /**
     * 删除上传的文件
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "delFile")
    public AjaxJson delFile(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            String id = oConvertUtils.getString(request.getParameter("id"));
            if (id != null && id != "") {
                systemService.deleteEntityById(ZyFileLogEntity.class, id);
                j.setMsg("文件删除成功");
                j.setSuccess(true);
            }
        } catch (Exception e) {
            j.setMsg(e.getMessage());
            j.setSuccess(false);
            e.printStackTrace();
        }
        return j;
    }

    /**
     * 删除上传的文件
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "delFileAndId")
    public AjaxJson delFileAndId(HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            String id = oConvertUtils.getString(request.getParameter("id"));
            String cbsServiceId = oConvertUtils.getString(request.getParameter("serviceId"));
            String type = oConvertUtils.getString(request.getParameter("type"));
            if (id != null && id != "") {
                systemService.deleteEntityById(ZyFileLogEntity.class, id);
                if (StringUtil.isNotEmpty(cbsServiceId)) {
                    if (type.equals("2")) {
                        systemService.updateBySqlString("update cbs_scene_report set repair_after_file = NULL where service_id = '" + cbsServiceId + "'");
                    } else if (type.equals("1")) {
                        systemService.updateBySqlString("update cbs_scene_report set repair_front_file = NULL where service_id = '" + cbsServiceId + "'");
                    }
                }
                j.setMsg("文件删除成功");
                j.setSuccess(true);
            }
        } catch (Exception e) {
            j.setMsg(e.getMessage());
            j.setSuccess(false);
            e.printStackTrace();
        }
        return j;
    }

    /**
     * HD查看完工单
     */
    @ResponseBody
    @RequestMapping(params = "lookReportByHD")
    public AjaxJson lookReportByHD(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        Map map = new HashMap();
        try {
            String id = request.getParameter("id");
            CbsServiceEntity cbsServiceEntity = systemService.getEntity(CbsServiceEntity.class, id);
            if (StringUtil.isNotEmpty(cbsServiceEntity.getReportType()) && cbsServiceEntity.getStatus().equals(StaticValue.ywc)) {
                if (cbsServiceEntity.getReportType() == 1) {
                    map.put("reportType", 1);
                    map.put("reportId", cbsServiceEntity.getId());
                } else if (cbsServiceEntity.getReportType() == 3) {
                    map.put("reportType", 3);
                    map.put("reportId", cbsServiceEntity.getId());
                } else if (cbsServiceEntity.getReportType() == 4) {
                    map.put("reportType", 4);
                    map.put("reportId", cbsServiceEntity.getId());
                } else if (cbsServiceEntity.getReportType() == 5) {
                    map.put("reportType", 5);
                    map.put("reportId", cbsServiceEntity.getId());
                } else if (cbsServiceEntity.getReportType() == 6) {
                    map.put("reportType", 6);
                    map.put("reportId", cbsServiceEntity.getId());
                } else if (cbsServiceEntity.getReportType() == 7) {
                    map.put("reportType", 7);
                    map.put("reportId", cbsServiceEntity.getId());
                }
                map.put("status", cbsServiceEntity.getStatus());
            } else {
                map.put("ty", 0);
            }

        } catch (Exception e) {
            e.getMessage();
        }
        ajaxJson.setAttributes(map);
        return ajaxJson;
    }

    /**
     * 判断完工报告是否已经填写
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "IfWriteReport")
    public AjaxJson IfWriteReport(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            String cbsId = request.getParameter("id");
            CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult(" from CbsServiceReportEntity where serviceId.id='" + cbsId + "'");
            if (cbsServiceReportEntity != null && cbsServiceReportEntity.getStatus() == 1) {
                ajaxJson.setSuccess(true);
            } else {
                ajaxJson.setSuccess(false);
            }
        } catch (Exception e) {
            ajaxJson.setSuccess(false);
            e.getMessage();
        }
        return ajaxJson;
    }

    /**
     * 工程师查看完工报告
     */
    @RequestMapping(params = "lookFinishReport")
    public ModelAndView lookFinishReport(HttpServletRequest req) {
        AjaxJson a = new AjaxJson();
        String id = req.getParameter("id");
        CbsServiceEntity cbsService = null;
        try {
            CbsServiceReportEntity cbsServiceReportEntity = systemService.singleResult("from CbsServiceReportEntity where serviceId.id = '" + id + "'");

            List<TSTerritory> territoryList = null;
            List<TSType> tsTypes = null;
            List<TSType> tsTypes2 = null;
            List<TSType> jgs = null;
            List<TSType> lws = null;
            List<TSType> gcq = null;
            List<TSType> photos = null;
            //上门次数
            if (StringUtil.isNotEmpty(id)) {
                List<Object[]> arrivalofabnormalEntities = systemService.findListbySql("select * from arrival_of_abnormal where service_id = '" + id + "' ORDER BY create_date DESC  LIMIT 1;");
                if (arrivalofabnormalEntities != null && arrivalofabnormalEntities.size() > 0) {
                    Integer num = (Integer) arrivalofabnormalEntities.get(0)[2];
                    req.setAttribute("signNumbers", "" + num + "次");
                    req.setAttribute("dateList", arrivalofabnormalEntities.get(0)[7]);
                } else {
                    req.setAttribute("signNumbers", "无");
                }
            }
            cbsService = systemService.singleResult("from CbsServiceEntity where id = '" + id + "'");
            //预约上门时间
            String orderTime = null;
            if (cbsService.getOrderTimeAgin() != null) {
                orderTime = String.valueOf(cbsService.getOrderTimeAgin());
            }
            if (cbsService.getOrderTimeAgin() == null && cbsService.getOrderTime() != null) {
                orderTime = String.valueOf(cbsService.getOrderTime());
            }
            req.setAttribute("orderTime", orderTime);
//        }
            //获取服务单中sn所属的类型
            ZyEquipmentSpareEntity zyEquipmentSpareEntity = systemService.singleResult("from ZyEquipmentSpareEntity where sn = '" + cbsService.getSn() + "'");
            if (zyEquipmentSpareEntity != null) {
                ZyEquipmentTypeEntity zyEquipmentTypeEntity = systemService.singleResult("from ZyEquipmentTypeEntity where id = '" + zyEquipmentSpareEntity.getType().getId() + "'");
                req.setAttribute("zyEquipmentTypeEntity", zyEquipmentTypeEntity);
                req.setAttribute("zyEquipmentSpareEntity", zyEquipmentSpareEntity);
            }
            //获取附加任务
            List<CbsServiceTaskMapEntity> cbsServiceTaskMapEntityList = systemService.findHql("from CbsServiceTaskMapEntity where serviceId.id='" + id + "' and type is null");
            List<CbsAttachTaskEntity> cbsAttachTaskEntities = new ArrayList<>();
            if (cbsServiceTaskMapEntityList != null && cbsServiceTaskMapEntityList.size() > 0) {
                for (CbsServiceTaskMapEntity serviceTaskMapEntity : cbsServiceTaskMapEntityList) {
                    List<CbsAttachTaskEntity> cbsAttachTaskEntityList = systemService.findHql("from CbsAttachTaskEntity where id='" + serviceTaskMapEntity.getAttachTaskId().getId() + "'");
                    cbsAttachTaskEntities.addAll(cbsAttachTaskEntityList);
                }
                req.setAttribute("cbsAttachTaskEntities", cbsAttachTaskEntities);
            }

            territoryList = systemService.findHql("from TSTerritory where territoryLevel = 1 order by territorySort");

            req.setAttribute("territoryList", territoryList);
            req.setAttribute("cbsService", cbsService);
            tsTypes = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='FWQPP'");
            req.setAttribute("types", tsTypes);
            tsTypes2 = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='GYPP'");
            req.setAttribute("types2", tsTypes2);
            jgs = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='jg_reform'");
            req.setAttribute("jgs", jgs);
            lws = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='lwzk'");
            req.setAttribute("lws", lws);
            gcq = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='GCQ'");
            List<Map<String, Object>> maps = systemService.findForJdbc("SELECT zy_equipment_type.id,zy_equipment_type.type_desc FROM zy_equipment_type LEFT JOIN zy_es_item i ON zy_equipment_type.id = i.type WHERE i.is_track = \"1\" and parent_id in (select id from zy_equipment_type where type_desc= '放映机备件') GROUP BY zy_equipment_type.type_desc ");
            req.setAttribute("maps", maps);
            req.setAttribute("gcq", gcq);
            photos = systemService.findHql("FROM TSType where TSTypegroup.typegroupcode='photoTypesOf'");
            req.setAttribute("photos", photos);
            List<Map<String, Object>> mapList = systemService.findForJdbc("select id,type_desc from zy_equipment_type where parent_id in (select id from zy_equipment_type where id  in (select id from zy_equipment_type where parent_id ='402883f3545bca4101545be730e0000b'))");
            req.setAttribute("mapList", mapList);
            List<String> dateList = systemService.findListbySql("select date from arrival_of_abnormal where service_id='" + cbsService.getId() + "' order by create_Date desc");
            if (dateList.size() != 0) {
                req.setAttribute("dateList", dateList.get(0));
            }
            req.setAttribute("cbsServiceReportEntity", cbsServiceReportEntity);
            //回显附件
            if (cbsService != null) {
                List<ZyFileLogEntity> zyFileLogEntityList = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC1'");
                req.setAttribute("zyFileLogEntityList", zyFileLogEntityList);
                List<ZyFileLogEntity> zyFileLogEntityList2 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC2'");
                req.setAttribute("zyFileLogEntityList2", zyFileLogEntityList2);
                List<ZyFileLogEntity> zyFileLogEntityList3 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC3'");
                req.setAttribute("zyFileLogEntityList3", zyFileLogEntityList3);
                List<ZyFileLogEntity> zyFileLogEntityList4 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC4'");
                req.setAttribute("zyFileLogEntityList4", zyFileLogEntityList4);
                List<ZyFileLogEntity> zyFileLogEntityList5 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC5'");
                req.setAttribute("zyFileLogEntityList5", zyFileLogEntityList5);
                List<ZyFileLogEntity> zyFileLogEntityList6 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC6'");
                req.setAttribute("zyFileLogEntityList6", zyFileLogEntityList6);
                List<ZyFileLogEntity> zyFileLogEntityList7 = systemService.findHql("from ZyFileLogEntity where businessKey = '" + cbsService.getId() + "' and businessType = 'XC7'");
                req.setAttribute("zyFileLogEntityList7", zyFileLogEntityList7);
            }
            req.setAttribute("type", "look");
            //获取备件使用情况
            if (cbsServiceReportEntity != null) {
                List<CbsSceneEquipmentUseEntity> cbsSceneEquipmentUseEntityList = systemService.findHql("from CbsSceneEquipmentUseEntity where sceneReportId.id = '" + cbsServiceReportEntity.getId() + "'");
                req.setAttribute("cbsSceneEquipmentUseEntityList", cbsSceneEquipmentUseEntityList);
            }
            if (cbsServiceReportEntity.getReportCategory() == 1) {
                return new ModelAndView("com/zybk/cbsServiceReport/referOrders-show");
            } else {
                return new ModelAndView("com/zybk/cbsServiceReport/remoteService-show");
            }
        } catch (Exception e) {
            e.getMessage();
        }
        return null;
    }
    //转换日期
//    @InitBinder
//    public void initBinder(WebDataBinder binder, WebRequest request) {
//
//        //转换日期
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
//    }

    /**
     * 现场服务完工报告编辑页面跳转
     *
     * @return
     */
    @RequestMapping(params = "goUpdate")
    public ModelAndView goUpdate(CbsServiceReportEntity cbsServiceReport, HttpServletRequest req) {
        if (StringUtil.isNotEmpty(cbsServiceReport.getId())) {
            cbsServiceReport = cbsServiceReportService.getEntity(CbsServiceReportEntity.class, cbsServiceReport.getId());
            req.setAttribute("cbsServiceReportPage", cbsServiceReport);
        }
        return new ModelAndView("com/zybk/report/cbsServiceReport-update");
    }

    /**
     * 更新现场服务完工报告
     *
     * @param
     * @return
     */
    @RequestMapping(params = "doUpdate")
    @ResponseBody
    public AjaxJson doUpdate(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        message = "现场服务完工报告更新成功";
        CbsServiceReportEntity t = cbsServiceReportService.get(CbsServiceReportEntity.class, cbsServiceReport.getId());
        try {
            MyBeanUtils.copyBeanNotNull2Bean(cbsServiceReport, t);
            cbsServiceReportService.saveOrUpdate(t);
            systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
        } catch (Exception e) {
            e.printStackTrace();
            message = "现场服务完工报告更新失败";
            throw new BusinessException(e.getMessage());
        }
        j.setMsg(message);
        return j;
    }


    /**
     * 删除现场服务完工报告
     *
     * @return
     */
    @RequestMapping(params = "doDel")
    @ResponseBody
    public AjaxJson doDel(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        cbsServiceReport = systemService.getEntity(CbsServiceReportEntity.class, cbsServiceReport.getId());
        message = "现场服务完工报告删除成功";
        try {
            cbsServiceReportService.delete(cbsServiceReport);
            systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
        } catch (Exception e) {
            e.printStackTrace();
            message = "现场服务完工报告删除失败";
            throw new BusinessException(e.getMessage());
        }
        j.setMsg(message);
        return j;
    }

    /**
     * 批量删除现场服务完工报告
     *
     * @return
     */
    @RequestMapping(params = "doBatchDel")
    @ResponseBody
    public AjaxJson doBatchDel(String ids, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        message = "现场服务完工报告删除成功";
        try {
            for (String id : ids.split(",")) {
                CbsServiceReportEntity cbsServiceReport = systemService.getEntity(CbsServiceReportEntity.class,
                        id
                );
                cbsServiceReportService.delete(cbsServiceReport);
                systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            message = "现场服务完工报告删除失败";
            throw new BusinessException(e.getMessage());
        }
        j.setMsg(message);
        return j;
    }

    /**
     * 导入功能跳转
     *
     * @return
     */
    @RequestMapping(params = "upload")
    public ModelAndView upload(HttpServletRequest req) {
        return new ModelAndView("com/zybk/report/cbsServiceReportUpload");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "exportXls")
    public String exportXls(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request, HttpServletResponse response
            , DataGrid dataGrid, ModelMap modelMap) {
        CriteriaQuery cq = new CriteriaQuery(CbsServiceReportEntity.class, dataGrid);
        org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cbsServiceReport, request.getParameterMap());
        List<CbsServiceReportEntity> cbsServiceReports = this.cbsServiceReportService.getListByCriteriaQuery(cq, false);
        modelMap.put(NormalExcelConstants.FILE_NAME, "现场服务完工报告");
        modelMap.put(NormalExcelConstants.CLASS, CbsServiceReportEntity.class);
        modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("现场服务完工报告列表", "导出人:" + ResourceUtil.getSessionUserName().getRealName(),
                "导出信息"));
        modelMap.put(NormalExcelConstants.DATA_LIST, cbsServiceReports);
        return NormalExcelConstants.JEECG_EXCEL_VIEW;
    }

    /**
     * 导出excel 使模板
     *
     * @param request
     * @param response
     */
    @RequestMapping(params = "exportXlsByT")
    public String exportXlsByT(CbsServiceReportEntity cbsServiceReport, HttpServletRequest request, HttpServletResponse response
            , DataGrid dataGrid, ModelMap modelMap) {
        modelMap.put(TemplateExcelConstants.FILE_NAME, "现场服务完工报告");
        modelMap.put(TemplateExcelConstants.PARAMS, new TemplateExportParams("Excel模板地址"));
        modelMap.put(TemplateExcelConstants.MAP_DATA, null);
        modelMap.put(TemplateExcelConstants.CLASS, CbsServiceReportEntity.class);
        modelMap.put(TemplateExcelConstants.LIST_DATA, null);
        return TemplateExcelConstants.JEECG_TEMPLATE_EXCEL_VIEW;
    }

    @RequestMapping(params = "choose")
    public ModelAndView choose(HttpServletRequest req) {
        String cbsId = req.getParameter("cbsId");
        req.setAttribute("cbsId", cbsId);
        String type = req.getParameter("type");
        req.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
        return new ModelAndView("com/zybk/cbsServiceReport/choose");
    }

    /**
     * 是否现场服务
     *
     * @return
     */
    @RequestMapping(params = "fieldservice")
    public ModelAndView fieldservice(HttpServletRequest request) {
        String cbsId = request.getParameter("id");
        request.setAttribute("cbsId", cbsId);
        String type = request.getParameter("type");
        request.setAttribute("type", StringUtil.isNotEmpty(type) ? type : "1");
        return new ModelAndView("com/zybk/cbsServiceReport/fieldservice");
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(params = "importExcel", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson j = new AjaxJson();

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<CbsServiceReportEntity> listCbsServiceReportEntitys = ExcelImportUtil.importExcelByIs(file.getInputStream(), CbsServiceReportEntity.class, params);
                for (CbsServiceReportEntity cbsServiceReport : listCbsServiceReportEntitys) {
                    cbsServiceReportService.save(cbsServiceReport);
                }
                j.setMsg("文件导入成功！");
            } catch (Exception e) {
                j.setMsg("文件导入失败！");
                logger.error(ExceptionUtil.getExceptionMessage(e));
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return j;
    }


    /**
     * 获取APP端保存成后，内容回显
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "getALLReport")
    public AjaxJson getALLReport(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        Map map = new HashMap();
        String id = request.getParameter("planIds"); //服务单id
        List<CbsServiceReportEntity> CbsServiceReportList = systemService.findByProperty(CbsServiceReportEntity.class, "serviceId.id", id);
        if (CbsServiceReportList.size() > 0) {
            map.put("CbsServiceReport", CbsServiceReportList.get(0));
            CbsServiceReportEntity cbsServiceReportEntity = CbsServiceReportList.get(0);
            List<CbsSceneEquipmentUseEntity> cbsSceneEquipmentUseList = systemService.findByProperty(CbsSceneEquipmentUseEntity.class, "sceneReportId.id", cbsServiceReportEntity.getId());
            if (cbsSceneEquipmentUseList.size() > 0) {
                map.put("cbsSceneEquipmentUseList", cbsSceneEquipmentUseList);
            }
            ajaxJson.setAttributes(map);
            if (StringUtil.isNotEmpty(cbsServiceReportEntity.getFinishTime())) {
                ajaxJson.setSuccess(false); //APP端已经提交过完工报告
            }
        }

        return ajaxJson;
    }


    /**
     * APP端根据服务单id，获取服务单状态，
     * 已经完工的单子，不能显示提交完工单
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(params = "getYNwg")
    public AjaxJson getYNwg(HttpServletRequest request) {
        AjaxJson ajaxJson = new AjaxJson();
        String id = request.getParameter("planIds"); //服务单id
        ajaxJson.setSuccess(false);
        if (StringUtil.isNotEmpty(id)) {
            String sql = "SELECT id from cbs_service WHERE id = '" + id + "' and ( `status`='" + StaticValue.ywc + "' or `status`='" + StaticValue.yqx + "' or speed_status='" + StaticValue.fwywc + "')";
            List<Map<String, Object>> list = systemService.findForJdbc(sql);
            if (list.size() > 0) {
                ajaxJson.setSuccess(true);
            } else {
                ajaxJson.setSuccess(false);
            }
        }
        return ajaxJson;
    }


    //定时任务：维修服务单状态变为已完成，如果三日内用户没有评价自动好评
    public void checkEvaluateStatus(final String serviceId) {
        System.out.println("检查评价状态");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                try {
                    List<CbsInspectionEvaluateEntity> evaluateEntity = systemService.findHql("from CbsInspectionEvaluateEntity where busType='type_2' and cbsService.id='" + serviceId + "'");
                    if (evaluateEntity != null && evaluateEntity.size() > 0) {
                        if (evaluateEntity.get(0) != null) {
                            if (StringUtil.isNotEmpty(evaluateEntity.get(0).getEvaluateState()) && evaluateEntity.get(0).getEvaluateState().equals("0")) {
                                wxCommonService.bestEvaluate(serviceId);
                                System.out.println("自动好评");
                            }
                        }

                    } else {
                        wxCommonService.bestEvaluate(serviceId);
                        System.out.println("自动好评");
                    }
                } catch (Exception e) {
                    System.err.println("自动评价失败");
                }

            }
        }, StaticValue.evaluateTime);


    }
}
